//加载字典
function dictJudge(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.options.sex;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'location/userregister/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id_', width: 50, key: true ,hidden:true},
			{ label: '注册用户名称', name: 'userName', index: 'user_name', width: 80 }, 			
			{ label: '身份证号码', name: 'idNumber', index: 'id_number', width: 80 }, 			
			{ label: '手机号码', name: 'phone', index: 'phone', width: 80 }, 			
			{ label: '车辆识别代码', name: 'vin', index: 'vin', width: 80 }, 			
			{ label: '车牌号', name: 'plateNumber', index: 'plate_number', width: 80 }, 			
			{ label: '银行卡号', name: 'bankCard', index: 'bank_card', width: 80,hidden:true },
			{ label: 'IMIE号', name: 'imei', index: 'imei', width: 80 }, 			
			{ label: '行车记录仪型号', name: 'model', index: 'model', width: 80 }, 			
			{ label: '行车记录仪编号', name: 'reportNumber', index: 'report_number', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80,formatter:formatSubscribeTime },
			{ label: '所属银行', name: 'bankName', index: 'bank_name', width: 80,hidden:true },
			{ label: '所属职业', name: 'vocation', index: 'vocation', width: 80,hidden:true },
			{ label: '车辆颜色', name: 'vehicleColor', index: 'vehicle_color', width: 80,hidden:true },
			{ label: '身份证扫描件', name: 'idScanUrl', index: 'id_scan_url', width: 80,hidden:true },
			{ label: '银行卡扫描件', name: 'bankScanUrl', index: 'bank_scan_url', width: 80,hidden:true },
			{ label: '其他资料', name: 'otherDocument', index: 'other_document', width: 80,hidden:true},
			{ label: '经度 由行车记录仪上传', name: 'lon', index: 'lon', width: 80,hidden:true },
			{ label: '纬度 由行车记录仪上传', name: 'lat', index: 'lat', width: 80,hidden:true },
			{ label: '经纬度更新时间', name: 'uploadTime', index: 'upload_time', width: 80 ,hidden:true}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		userRegister: {},
        queryCondition:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.userRegister = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    if(vm.userRegister.id ==null) {
                var bootstrapValidator = $("#userRegisterForm").data('bootstrapValidator');
                bootstrapValidator.validate();
                if (!bootstrapValidator.isValid()) {
                    return;
                }
            }else{
                $('#userRegisterForm').data('bootstrapValidator').enableFieldValidators('bankScanUrl', true);
                $('#userRegisterForm').data('bootstrapValidator').enableFieldValidators('idScanUrl', true);
            }
			var url = vm.userRegister.id == null ? "location/userregister/save" : "location/userregister/update";
            var fd = new FormData(document.getElementById('userRegisterForm'));
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: fd,
                processData: false,  // 告诉jQuery不要去处理发送的数据
                contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "location/userregister/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "location/userregister/info/"+id, function(r){
                vm.userRegister = r.userRegister;
            });
		},
		reload: function (event) {
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            vm.showList = true;
            postData = {
                "userName":vm.queryCondition.userName==null?undefined:vm.queryCondition.userName,
                "idNumber":vm.queryCondition.idNumber==null?undefined:vm.queryCondition.idNumber,
                "phone":vm.queryCondition.phone==null?undefined:vm.queryCondition.phone,
                "imei":vm.queryCondition.imei==null?undefined:vm.queryCondition.imei,
                "plateNumber":vm.queryCondition.plateNumber==null?undefined:vm.queryCondition.plateNumber
            };
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:postData
            }).trigger("reloadGrid");
		},
        download:function(){
            window.open(vm.userRegister.otherFilePath);
        }
	}
});